// Copyright (c) 2000-2002 N.Mikhailov, mn@ixbt.com, http://forum.iXBT.com

var old_action = "";
var IE5 = (navigator.userAgent.indexOf('MSIE')!=-1) && (navigator.userAgent.indexOf('Opera')==-1);
var Opera7 = (navigator.userAgent.indexOf('Opera')!=-1) && (navigator.userAgent.charAt(navigator.userAgent.indexOf('Opera')+6)>=7);

function p1(text){ 
if (text!="") paste("[b]"+text+"[/b]\n", 1);
}

function p2(text){ 
if (text!="") paste("[i]"+text+"[/i]\n", 0);
}

function paste(text, flag){ 
if ((document.selection)&&(flag)) {
	document.postform.message.focus();
	document.postform.document.selection.createRange().text = text;
} else document.postform.message.value += text;
}

function get_selection() {
   if (document.getSelection){
	selection = document.getSelection();
	if (!Opera7){
		selection = selection.replace(/\r\n\r\n/gi, "_doublecaret_");
		selection = selection.replace(/\r\n/gi, " ");
	       while (selection.indexOf("  ") !=-1) selection = selection.replace(/  /gi, ""); 
		selection = selection.replace(/_doublecaret_/gi, "\r\n\r\n");
	}
  } else
      selection = document.selection.createRange().text;
}

function setup_postform(enablefocus){
if (enablefocus) {document.postform.UserName.focus()}
var testUserName = getCookie("user")
var testPassword = getCookie("pass")
if ((document.cookie)&&(testUserName != null)) {
	document.postform.UserName.value = testUserName
	if (testPassword!=null) {document.postform.Password.value = testPassword}
	if (enablefocus) {document.postform.message.focus()}
}
if (getCookie("setup")!=null){
	if (getCookie("setup").indexOf("D") != -1) {document.postform.without_smilies.checked = true}
}
if ((!enablefocus)&&(document.postform.message.style!=null)){ 
//	var i=1;
//	while (document.all("tbl"+i) != null) {document.all("tbl"+i).style.tableLayout="fixed"; i++}

	var testform_cookie = getCookie('setup2')
	if (testform_cookie!=null){
		form_cookie=testform_cookie.split("|");
		if ((form_cookie[3] != null)&&(form_cookie[3]>=20)&&(form_cookie[3]<=250)) {document.postform.message.cols=form_cookie[3]} else {document.postform.message.style.width="100%"};
		if ((form_cookie[4] != null)&&(form_cookie[4]>=5)&&(form_cookie[4]<=100)) document.postform.message.rows=form_cookie[4]
		if ((form_cookie[5] != null)&&(form_cookie[5]>=0)) document.postform.message.style.fontSize=form_cookie[5]
		if ((form_cookie[6] != null)&&(form_cookie[6]!='')) document.postform.message.accessKey=form_cookie[6]
		if ((form_cookie[7] != null)&&(form_cookie[7]>50)&&(IE5 || Opera7)) document.postform.message.style.height=form_cookie[7]
	} else {document.postform.message.style.width="100%"}
}

switch(navigator.appName) {
   case "Microsoft Internet Explorer":
      Key = "event.ctrlKey && event.keyCode == 13";
      document.onkeydown = get_key;
      break;
   case "Netscape":
      Key = "(e.modifiers == 2 && e.which == 10) || (e.ctrlKey && e.which == 13)";
      document.captureEvents(Event.KEYDOWN);
      document.onkeydown = get_key;
      break;
}
}

function get_key(e) {
if (eval(Key)) {
	if (check_postform()){
		check_submit();
		document.postform.submit();
		submit_once(document.postform);
	} else {return false}
}
}


function setup_loginform(){
var testUserName = getCookie("user")
var testPassword = getCookie("pass")
if ((document.cookie)&&(testUserName != null)) {
	document.loginform.UserName.value = testUserName
	if (testPassword!=null) {document.loginform.Password.value = testPassword}
	}
}

function check_postform(){
var max = 42000
if (document.postform.message.value.length == 0){
	alert('Напишите, пожалуйста, что-нибудь!');
	return false;
}
if (document.postform.message.value.length > max){
	alert("Размер Вашего сообщения превышает максимум на "+(document.postform.message.value.length-max)+" символов! Пожалуйста, уменьшите его, разбив на несколько частей!");
	return false;
}
if (document.postform.UserName.value.length == 0){
	alert('Вы забыли указать своё имя!');
	return false;
}
return true;
}


function check_loginform(){
if ((document.loginform.UserName.value.length == 0)||(document.loginform.Password.value.length == 0)){
	alert('Вы заполнили не все поля! Укажите своё имя и пароль!')
	return false;
 }
return true;
}

function check_submit() {
if (old_action!="") document.postform.id.value=old_action
document.postform.target="_self";
}

function submit_once(theform){
if (theform.target=="preview") {return}
if (document.all||document.getElementById){
for (i=0;i<theform.length;i++){
var tempobj=theform.elements[i]
if(tempobj.type.toLowerCase()=="submit"||tempobj.type.toLowerCase()=="reset") {tempobj.disabled=true}
}
}
}

function getCookie(name) {
var prefix = name + "="
var StartIndex = document.cookie.indexOf(prefix)
if (StartIndex == -1)
return null
var EndIndex = document.cookie.indexOf(";", StartIndex + prefix.length)
if (EndIndex == -1)
EndIndex = document.cookie.length
return unescape(document.cookie.substring(StartIndex + prefix.length, EndIndex))
}
